#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sqlite3
import logging.config
from db.sqllite3_wrapper import DbAccess

class DbRepository(object):
    """Repository layer for DB"""

    def __init__(self):
        self.__logger = logging.getLogger(__name__) 
        self.__dbAccess = DbAccess("LocalDb")

    def getDbAccess(self):
        return self.__dbAccess

    def insertOrUpdateDistrictTrend(self, trends):  
        '''
        Update trends for specified districts and dates
        '''
        self.__logger.info('updating %d district trends.', len(trends))
        return self.__dbAccess.executemany('''REPLACE INTO DistrictTrend (district,
				    [date],
				    avgPrice,
				    onsaleNum ,	
				    dealNum ,
				    showNum ,
				    totalDealNum,
				    dealNumOf90Days)
                VALUES (:district,
				    :date,
				    :avgPrice ,
				    :onsaleNum ,	
				    :dealNum ,
				    :showNum ,
				    :totalDealNum,
				    :dealNumOf90Days)''', 
                    trends)

    def insertOrUpdateDailyIndex(self, dailyIndex):
        '''Update a single daily index for a specified date'''
        return self.__dbAccess.execute('''REPLACE INTO DailyIndex
                    (
	                    [date],
	                    newHouses,	--新房数量
	                    newClients, --新客数
	                    showNums, --带看数
	                    dealNums, --成交数
	                    dealNumsOfLasy90Days, --最近90天的成交数
	                    onSaleNum,	--可售房数
	                    totalSaledNum, --总共已售数
	                    dongchengDealNumsOfLast90Days, --东城成交数
	                    xichengDealNumsOfLast90Days, --西城成交数
	                    chaoyangDealNumsOfLast90Days --朝阳成交数
                    )
                    VALUES
                    (
	                    :date,
	                    :newHouses,
	                    :newClients, 
	                    :showNums, 
	                    :dealNums, 
	                    :dealNumsOfLasy90Days, 
	                    :onSaleNum,
	                    :totalSaledNum, 
	                    :dongchengDealNumsOfLast90Days, 
	                    :xichengDealNumsOfLast90Days, 
	                    :chaoyangDealNumsOfLast90Days 
                    )''', dailyIndex)

    def insertOrUpdateFullIndex(self, fullIndex):
        '''
        Update full index per month
        '''
        return self.__dbAccess.execute('''REPLACE INTO FullIndex
                                (
	                                [year],  
	                                [month],
	                                dealPrice, --成交均价
	                                listPrice, --挂牌均价 
	                                newHourseNum, --新房数量
	                                showNums --带看数量	
                                )
                                VALUES
                                (
	                                :year,  
	                                :month,
	                                :dealPrice, 
	                                :listPrice, 
	                                :newHourseNum, 
	                                :showNums 
                                )
                    ''', fullIndex)
        
        